package com.example.consume;

import com.example.utils.RabbitMqUtils;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;

import java.io.IOException;

public class ConsumerB {

    //交换机名称
    private static final String EXCHANGE = "my.topic";

    public static void main(String[] args) throws IOException {
        //获取通道
        Channel channel = RabbitMqUtils.getChannel();
        //声明交换机
        channel.exchangeDeclare(EXCHANGE, BuiltinExchangeType.TOPIC);
        //创建队列
        channel.queueDeclare("Q2", false, false, false, null);
        //交换机绑定队列
        channel.queueBind("Q2", EXCHANGE, "*.*.rabbit");
        channel.queueBind("Q2", EXCHANGE, "lazy.#");
        //消费消息
        DeliverCallback deliverCallback = (consumerTag, message) -> System.out.println("NodeB:" + new String(message.getBody()));
        CancelCallback cancelCallback = consumerTag -> System.out.println("消息消费异常");
        channel.basicConsume("Q2", true, deliverCallback, cancelCallback);
    }
}
